package fr.umlv.m2.jee.pmlv.service.note.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import fr.umlv.m2.jee.pmlv.persistence.note.UserThemeNote;
import fr.umlv.m2.jee.pmlv.persistence.note.dao.HibernateUserThemeNoteDAO;
import fr.umlv.m2.jee.pmlv.persistence.subject.AbstractTheme;
import fr.umlv.m2.jee.pmlv.persistence.subject.dao.HibernateAbstractThemeDAO;
import fr.umlv.m2.jee.pmlv.service.note.DisplayUserThemeNote;
import fr.umlv.m2.jee.pmlv.service.note.IDisplayUserThemeNoteService;
import java.util.HashMap;

@Service("defautDisplayUserThemeNoteService")
public class DefautDisplayUserThemeNoteService implements IDisplayUserThemeNoteService {
	
    @Autowired
    @Qualifier("hibernateUserThemeNoteDAO")
    private HibernateUserThemeNoteDAO dao;
    @Autowired
    @Qualifier("hibernateAbstractThemeDAO")
    private HibernateAbstractThemeDAO tdao;

    @Override
    @Transactional(readOnly=true)
    public List<DisplayUserThemeNote> getAllDisplayUserThemeNote() {
            ArrayList<DisplayUserThemeNote> result = new ArrayList<DisplayUserThemeNote>(16);
            for(UserThemeNote note : dao.findAll()) {
                    result.add(new DisplayUserThemeNote(note));
            }
            return result;
    }

    @Override
    @Transactional(readOnly=true)
    public DisplayUserThemeNote getDisplayUserThemeNoteById(String id) {
            return new DisplayUserThemeNote(dao.findById(Long.parseLong(id)));
    }

    @Override
    @Transactional(readOnly=true)
    public List<DisplayUserThemeNote> getDisplayUserThemeNoteByUidAndByYear(String uid, int year) {
            ArrayList<DisplayUserThemeNote> result = new ArrayList<DisplayUserThemeNote>(16);
            for(UserThemeNote note : dao.findByUserAndByYear(uid,year)) {
                    result.add(new DisplayUserThemeNote(note));
            }
            return result;
    }

    @Override
    @Transactional(readOnly=true)
    public DisplayUserThemeNote getDisplayUserThemeNoteByUidAndThemeAndByYear(String uid, Long themeId, int year) {
        return new DisplayUserThemeNote(dao.findByUserAndThemeAndByYear(uid, themeId, year));
    }

    @Override
    @Transactional(readOnly=true)
    public List<DisplayUserThemeNote> getDisplayUserThemeNoteByThemeAndByYear(Long themeId, int year) {
        ArrayList<DisplayUserThemeNote> result = new ArrayList<DisplayUserThemeNote>(16);
        for(UserThemeNote note : dao.findByThemeAndByYear(themeId,year)) {
                result.add(new DisplayUserThemeNote(note));
        }
        return result;
    }

    @Override
    @Transactional(readOnly=true)
    public void markTheme(long idTheme, HashMap<String, Float> notes) {
        AbstractTheme theme = tdao.findById(idTheme);
        for(String s:notes.keySet())
        {
            if(notes.get(s)==null)
            {
                if(dao.findByUserAndThemeAndByYear(s, idTheme, theme.getYear()) != null)
                    dao.remove( dao.merge( dao.findByUserAndThemeAndByYear(s, idTheme, theme.getYear()) ) );
            }
            else
            {
                if(dao.findByUserAndThemeAndByYear(s, idTheme, theme.getYear()) != null)
                {
                    UserThemeNote note = dao.findByUserAndThemeAndByYear(s, idTheme, theme.getYear());
                    note.setNote(notes.get(s));
                    dao.merge(note);
                }
                else
                {
                    UserThemeNote note = new UserThemeNote();
                    note.setNote(notes.get(s));
                    note.setTheme( theme );
                    note.setUid(s);
                    dao.persist(note);
                }
            }
        }
    }
}
